package cn.kgc.springmvc.day04.controller;

import cn.kgc.springmvc.day04.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/11/13
 * @description 非法请求的拦截
 * <p>
 * 1.  servlet   自定义 filter    过滤器      tomcat
 * doFilter()   -> chain.doFilter()  放行   session   user
 * 2.  拦截器  interceptor     springmvc
 */
@RestController
@RequestMapping("user")
public class UserController {


    // 登录
    @RequestMapping("login")
    public String login(User user, HttpSession session) {

        if ("root".equals(user.getUsername()) && "root".equals(user.getPassword())) {
            session.setAttribute("authentication", "true");
            return "login success";
        }

        return "login fail";
    }


    // 注册
    @RequestMapping("register")
    public String register(User user) {

        return "register success";
    }

    // 查询
    @RequestMapping("list")
    public String userList() {

        return "select userList success";
    }


    // 查询
    @RequestMapping("delete")
    public String deleteUser(Integer id) {


        if (id < 0) {
            throw new NullPointerException();
        }

        return "delete user by id " + id;
    }


}
